/**
 * 
 */
package it.fuesi.signit.adapters;

import it.fuesi.signit.CurrentSettings;
import it.fuesi.signit.R;
import it.fuesi.signit.model.Signature;

import java.util.ArrayList;
import java.util.List;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @author pascotto
 *
 */
public class SignaturesAdapter extends BaseAdapter {

	private List<Signature> data;
	private LayoutInflater inflater;
	
	
	public SignaturesAdapter(LayoutInflater inflater) {
		this.inflater = inflater;
	}
	
	public void clear(){
		if(data != null)
			data.clear();
	}
	
	public void fetch(){
		clear();
		final List<Signature> signatures = CurrentSettings.instance().getSignatures();
		if(signatures != null){
			for(Signature s : signatures)
				add(s);
		}
		
		if(signatures == null || signatures.size() == 0)
			add(new Signature("Default signature"));
		
		notifyDataSetChanged();
	}
	
	public void add(Signature signature){
		if(data == null)
			data = new ArrayList<Signature>();
		
		data.add(signature);
	}

	@Override
	public int getCount() {
		if(data == null)
			return 0;
		
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.signature_list_item, null);
		
		
		final Signature signature = (Signature)getItem(position);
		
		final TextView sign = (TextView)vi.findViewById(R.id.signature_text);
		sign.setText(signature.getSignature());
		
		
		return vi;
	}
	

}
